package ar.edu.unq.tpi.pconc.tpconcurbattle;

import ar.edu.unq.tpi.pconc.Channel;
import ar.edu.unq.tpi.pconc.tpconcurbattle.utils.Util;

public abstract class Bloqueable {
	
	private Channel<String> bloqueo;
	
	private int id;

	private Channel<String> recepcionDeUnidades;
	
	public Bloqueable() {
		super();
		
		Channel<String> serverReceive = new Channel<String>(Util.SERVER_CHANNEL_RECIEVE);
		Channel<Integer> serverSend = new Channel<Integer>(Util.SERVER_CHANNEL_SEND);
		
		serverReceive.send("new");
		int n = serverSend.receive();
		
		this.id = n;
		
		this.bloqueo = new Channel<String>(n);
		
		System.out.println("Ubicación " + this.id + " canal " + n);
		
		serverReceive.send("new");
		n = serverSend.receive();
		
		this.recepcionDeUnidades = new Channel<String>(n);
		
		this.bloqueo.send("Llave"); // Con esto la locación está lista para ser invadida.
	}
	
	public Bloqueable(int id){
		this();
		this.id = id;
	}

	private Channel<String> getBloqueo() {
		return this.bloqueo;
	}

	public Channel<String> getRecepcionDeUnidades() {
		return this.recepcionDeUnidades;
	}
	
	public int getId() {
		return this.id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public void bloquear() {
		this.getBloqueo().receive();
	}
	
	public void desbloquear() {
		this.getBloqueo().send("Llave"); // Llave, abrir puerta
	}

}
